﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class BaoGia : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Download Báo giá - Văn Phòng Phẩm Mỹ Đình";
            lblInfo.Text = new sysConfigBL().Select(new sysConfigDO { ConfigID = 8 }).ConfigValue;
            if (Session["IDDN"] != null)
            {
                pnTT.Visible = false;
            }
            else
            {
                pnTT.Visible = true;
            }
        }

        private void FileDownload(string filename)
        {
            Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename= /Media/" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
            Response.TransmitFile(filename);
        }
        void StreamToFileAttachment(string fileName)
        {
            FileStream str = new FileStream("Media/" + fileName, FileMode.Open);
            byte[] buf = new byte[str.Length];  //declare arraysize
            str.Read(buf, 0, buf.Length);
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
            Response.AddHeader("Content-Length", str.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.OutputStream.Write(buf, 0, buf.Length);
            Response.End();
        }
        public void FileDown(string filename)
        {
            FileStream MyFileStream = new FileStream("Media/" + filename, FileMode.Open);
            long FileSize;
            FileSize = MyFileStream.Length;
            byte[] Buffer = new byte[(int)FileSize];
            MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
            MyFileStream.Close();
            Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename= /Media/" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
            Response.BinaryWrite(Buffer);
        }

        protected void ButtonSend_Click(object sender, EventArgs e)
        {
            ccJoin.ValidateCaptcha(txtbv.Text);
            if (!ccJoin.UserValidated)
            {
                Response.Write("<script language='javascript'>alert('Mã bảo vệ chưa chính xác..!!..')</script>");
                return;
            }
            else
            {
                //FileStream fs = new FileStream("F:/ses-thuyet/VPP-thuyet/SES.CMS/Media/" + new sysConfigBL().Select(new sysConfigDO{ConfigID=12}).ConfigValue, FileMode.Open);
                //StreamToFileAttachment(fs, "TechBrij.xls");
                //fs.Close();
                //Response.Redirect("/HandlerForMyFE/Downloadpath=" + HttpUtility.UrlEncode("/Media/BaoGia.xls"));
                try
                {
                    StreamToFileAttachment(new sysConfigBL().Select(new sysConfigDO { ConfigID = 12 }).ConfigValue);
                }
                catch (Exception ee)
                {
                    lbLoi.Text = ee.ToString();
                }
            }
        }
    }
}
